<template>
	<view class="ez-qrcode-content">
		<canvas class="tki-qrcode-canvas" canvas-id="qrcode-canvas" :style="{width:size+'px',height:size+'px'}" />
		<!-- <image :src="src" /> -->
	</view>
</template>

<script>
	import QRCode from "../../library/qrcode.js";
	let qrcode;
	export default{
		data() {
			return {
				src: "",
			}
		},
		methods: {
			/* 生成二维码 */
			makeCode() {
				let that = this
				if (!this._empty(this.content)) {
					qrcode = new QRCode({
						context: that, // 上下文环境
						canvasId: "qrcode-canvas", // canvas-id
						usingComponents: that.usingComponents, // 是否是自定义组件
						showLoading: true, // 是否显示loading
						loadingText: "二维码生成中", // loading文字
						text: that.content, // 生成内容
						size: Number(that.size), // 二维码大小
						background: that.background, // 背景色
						foreground: that.foreground, // 前景色
						pdground: that.pdground, // 定位角点颜色
						correctLevel: that.lv, // 容错级别
						image: that.icon, // 二维码图标
						imageSize: Number(that.iconSize),// 二维码图标大小
						cbResult: function (res) { // 生成二维码的回调
							if(typeof(res) == "string")that.$emit("getQrcode" , res)
						},
					});
				} else {
					uni.showToast({
						title: '二维码内容不能为空',
						icon: 'none',
						duration: 2000
					});
				}
			},
			_empty(v) {
				let tp = typeof v,
					rt = false;
				if (tp == "number" && String(v) == "") {
					rt = true
				} else if (tp == "undefined") {
					rt = true
				} else if (tp == "object") {
					if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
				} else if (tp == "string") {
					if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
				} else if (tp == "function") {
					rt = false
				}
				return rt
			}
		},
		created() {
			/* console.log(this.content)
			this.makeCode(); */
		},
		watch: {
			content: {
				handler() {
					console.log(this.content)
					this.makeCode()
				},
				immediate: true
			}
		},
		props: {
			content: {	//二维码内容
				type: String,
				defalut: ""
			},
			size: {	//二维码尺寸
				type: [String , Number],
				default: 200
			},
			icon: {	//二维码图标
				type: String,
				default: ""
			},
			iconSize: {	//图标尺寸
				type: [Number , String],
				default: 40
			},
			background: {	//背景色
				type: String,
				default: "#ffffff"
			},
			foreground: {	//前景色
				type: String,
				default: "#000000"
			},
			pdground: {	//定位角点颜色
				type: String,
				default: "#000000"
			},
			lv: {	//容错级别
				type: Number,
				default: 3
			},
		}
	}
</script>

<style>
</style>
